using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using FireStarterModeller.Test.BO;
using FireStarterModeller.UI;
using Habanero.Faces.Base;
using Habanero.Faces.Win;
using NUnit.Framework;

namespace FireStarterModeller.Test.UI
{
    [TestFixture]
    public class TestUIUtilities
    {
        [Test]
        public void Test_GetGridWidthToFitColumns_OneColumn()
        {
            //---------------Set up test pack-------------------
            IReadOnlyGrid grid = new ReadOnlyGridWin();
            grid.RowHeadersWidth = TestUtilsShared.GetRandomInt(100, 200);
            IDataGridViewColumn column = AddColumnWithRandomWidth(grid);
            //-------------Assert Preconditions -------------

            //---------------Execute Test ----------------------
            int width = UIUtilities.GetGridWidthToFitColumns(grid);
            //---------------Test Result -----------------------
            Assert.AreEqual(grid.RowHeadersWidth + column.Width, width);
        }

        [Test]
        public void Test_GetGridWidthToFitColumns_NoHeader_OneColumn()
        {
            //---------------Set up test pack-------------------
            IReadOnlyGrid grid = new ReadOnlyGridWin();
            grid.RowHeadersWidth = TestUtilsShared.GetRandomInt(100, 200);
            grid.RowHeadersVisible = false;
            IDataGridViewColumn column = AddColumnWithRandomWidth(grid);
            //-------------Assert Preconditions -------------

            //---------------Execute Test ----------------------
            int width = UIUtilities.GetGridWidthToFitColumns(grid);
            //---------------Test Result -----------------------
            Assert.AreEqual(column.Width, width);
        }

        [Test]
        public void Test_GetGridWidthToFitColumns_ManyColumns()
        {
            //---------------Set up test pack-------------------
            IReadOnlyGrid grid = new ReadOnlyGridWin();
            grid.RowHeadersWidth = TestUtilsShared.GetRandomInt(100, 200);
            int columnsWidth = 0;
            for (int i = 0; i < TestUtilsShared.GetRandomInt(2, 10); i++)
            {
                IDataGridViewColumn column = AddColumnWithRandomWidth(grid);
                columnsWidth += column.Width;
            }
            //-------------Assert Preconditions -------------

            //---------------Execute Test ----------------------
            int width = UIUtilities.GetGridWidthToFitColumns(grid);
            //---------------Test Result -----------------------
            Assert.AreEqual(grid.RowHeadersWidth + columnsWidth, width);
        }

        [Test]
        public void Test_GetGridWidthToFitColumns_NoHeader_ManyColumns()
        {
            //---------------Set up test pack-------------------
            IReadOnlyGrid grid = new ReadOnlyGridWin();
            grid.RowHeadersWidth = TestUtilsShared.GetRandomInt(100, 200);
            grid.RowHeadersVisible = false;
            int columnsWidth = 0;
            for (int i = 0; i < TestUtilsShared.GetRandomInt(2, 10); i++)
            {
                IDataGridViewColumn column = AddColumnWithRandomWidth(grid);
                columnsWidth += column.Width;
            }
            //-------------Assert Preconditions -------------

            //---------------Execute Test ----------------------
            int width = UIUtilities.GetGridWidthToFitColumns(grid);
            //---------------Test Result -----------------------
            Assert.AreEqual(columnsWidth, width);
        }

        [Test]
        public void Test_GetGridWidthToFitColumns_ManyColumns_SomeHidden()
        {
            //---------------Set up test pack-------------------
            IReadOnlyGrid grid = new ReadOnlyGridWin();
            grid.RowHeadersWidth = TestUtilsShared.GetRandomInt(100, 200);
            int columnsWidth = 0;
            bool visible = false;
            for (int i = 0; i < TestUtilsShared.GetRandomInt(4, 10); i++)
            {
                IDataGridViewColumn column = AddColumnWithRandomWidth(grid);
                column.Visible = visible;
                if (visible) columnsWidth += column.Width;
                visible = !visible;
            }
            //-------------Assert Preconditions -------------

            //---------------Execute Test ----------------------
            int width = UIUtilities.GetGridWidthToFitColumns(grid);
            //---------------Test Result -----------------------
            Assert.AreEqual(grid.RowHeadersWidth + columnsWidth, width);
        }

        [Test]
        public void Test_GetGridWidthToFitColumns_NoHeader_ManyColumns_SomeHidden()
        {
            //---------------Set up test pack-------------------
            IReadOnlyGrid grid = new ReadOnlyGridWin();
            grid.RowHeadersWidth = TestUtilsShared.GetRandomInt(100, 200);
            grid.RowHeadersVisible = false;
            int columnsWidth = 0;
            bool visible = false;
            for (int i = 0; i < TestUtilsShared.GetRandomInt(4, 10); i++)
            {
                IDataGridViewColumn column = AddColumnWithRandomWidth(grid);
                column.Visible = visible;
                if (visible) columnsWidth += column.Width;
                visible = !visible;
            }
            //-------------Assert Preconditions -------------

            //---------------Execute Test ----------------------
            int width = UIUtilities.GetGridWidthToFitColumns(grid);
            //---------------Test Result -----------------------
            Assert.AreEqual(columnsWidth, width);
        }

        private static IDataGridViewColumn AddColumnWithRandomWidth(IReadOnlyGrid grid)
        {
            string columnName = TestUtilsShared.GetRandomString();
            grid.Columns.Add(columnName,columnName);
            IDataGridViewColumn column = grid.Columns[columnName];
            column.Width = TestUtilsShared.GetRandomInt(100, 200);
            return column;
        }
    }
}
